#lang racket

(define (expt base n) (expt-iter base n 1 n base 1))

(define (expt-iter base n exp rem r acc)
  (cond ((> exp n) acc)
        ((= (remainder rem 2) 1) (expt-iter base n (+ exp exp) (floor (/ rem 2)) (* r r) (* acc r)))
        (else (expt-iter base n (+ exp exp) (floor (/ rem 2)) (* r r) acc))))

(expt 12 7)